﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Data;

namespace TransportAnalizer_2._0.Views
{
        [ValueConversion(typeof(System.Data.Entity.Spatial.DbGeography), typeof(String))]
        public class GeographyValueConverter : IValueConverter
        {
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
            {
                System.Data.Entity.Spatial.DbGeography location = (System.Data.Entity.Spatial.DbGeography)value;
                return location.Longitude.ToString().Replace(',', '.') + " , " + location.Latitude.ToString().Replace(',', '.');
                
            }

            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
            {
                string strValue = value as string;
                System.Data.Entity.Spatial.DbGeography resultLocation;
                strValue = strValue.Trim();
                strValue = strValue.Replace(",", "");
                strValue = strValue.Replace("  ", " ");
                strValue ="POINT("+ strValue + ")";
                
                try
                {
                    resultLocation = System.Data.Entity.Spatial.DbGeography.FromText(strValue);
                    return resultLocation;
                }
                catch
                {
                    return DependencyProperty.UnsetValue;
                }
                
            }
        }
    
}
